package gw.com.android.ui.chart.views;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gwtsz.chart.output.utils.ChartConfig;
import com.gwtsz.chart.output.utils.ChartRendererFactory;
import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppMain;
import gw.com.android.ui.dialog.BaseDialog;
import www.com.library.util.CommonUtils;
import www.com.library.view.RecyclerClickListener;
import www.com.library.view.TintTextView;

/**
 * k线数据类型弹层
 *
 */
public class ChartKTypePopWindow extends BaseDialog{

	protected String[] mArray;
	protected RecyclerView popListView;// pop上的listview
	protected RecyclerClickListener callback;// 点击事件的回调函数
	protected RecyclerView.Adapter modeListAdapter;

	/**
	 * 默认有提示信息
	 *
	 * @param context
	 */
	public ChartKTypePopWindow(Context context, RecyclerClickListener callback) {
		super(context);
		this.callback = callback;
	}

	@Override
	public void initParam() {
		mCustomViewResId = R.layout.dialog_chart_k_type;
		mHasNegButton = false;
		int padding = (int) mOwnerAct.getResources().getDimension(R.dimen.space_c);
		setPadding(padding, 0, padding, 0);
	}

	@Override
	public void inflaterCustomView(View dialogView) {
		mBtnLayout.setVisibility(View.GONE);
		mArray = AppMain.getAppArrayString(R.array.chart_k_types);
		popListView = (RecyclerView) dialogView.findViewById(R.id.pop_list);
		modeListAdapter = new DataAdapter(mOwnerAct);
		popListView.setAdapter(modeListAdapter);
		// 线性布局管理器
		GridLayoutManager layoutManager = new GridLayoutManager(mOwnerAct, 3);
		// 设置布局管理器
		popListView.setLayoutManager(layoutManager);
		dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

	/**
	 * 列表适配器
	 * @author reeta
	 *
	 */
	public class DataAdapter extends RecyclerView.Adapter{
		private LayoutInflater mInflater;

		public DataAdapter(Activity act) {
			mInflater = LayoutInflater.from(act);
		}

		public String getItem(int position) {
			if (position < 0 || position >= mArray.length)
				return null;
			return mArray[position].toString();
		}

		@Override
		public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View itemView = mInflater.inflate(R.layout.list_item_chart_dialog, parent,false);
			return new ItemView(itemView);
		}

		@Override
		public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
			ItemView viewHold = (ItemView) holder;
			if (viewHold.mView != null) {
				viewHold.mView.setText(getItem(position));
				viewHold.mView.setTag(position);
				ChartRendererFactory.ChartRendererType type = ChartConfig.instance().getChartRendererType();
				if(type.typeId() == position){
					viewHold.mView.setSelected(true);
				}else{
					viewHold.mView.setSelected(false);
				}
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public int getItemCount() {
			if(null != mArray){
				return mArray.length;
			}
			return 0;
		}

		class ItemView extends RecyclerView.ViewHolder{

			@BindView(R.id.item_title)
			TintTextView mView;

			public ItemView(View itemView) {
				super(itemView);
				ButterKnife.bind(this, itemView);
			}

			@OnClick(R.id.item_layout)
			void onClick() {
				if(CommonUtils.isFastDoubleClick()){
					return;
				}
				if(null != callback){
					int position = (int) mView.getTag();
					dismiss();
					callback.onClick(position, null);
				}
			}
		}
	}
}
